//导入request.ts请求服务
import request from '../utils/request.ts'

//提供调用注册接口
export const userRegisterService = (registerData: any) => {
  //借助URLSearchParams完成传递参数
  const params = new URLSearchParams()
  for (const key in registerData) {
    params.append(key, registerData[key])
  }
  console.log('注册参数:', params.toString())
  return request({
    url: '/user/register',
    method: 'post',
    data: params
  })
}

//提供调用密码登录接口
export const userLoginService = (loginData: any) => {
  const params = new URLSearchParams()
  for (const key in loginData) {
    params.append(key, loginData[key])
  }
  console.log('登录参数:', params.toString())

  return request.post('/user/login', params)
}
//提供验证码登录 
export const userCodeLoginService = (loginData: any) => { 
  const params = new URLSearchParams() 
  for (const key in loginData) { 
    params.append(key, loginData[key]) 
  } 
  console.log('验证码登录参数:', params.toString()) 
  return request.post('/user/codeLogin', params) 
}

//提供发送注册验证码（用于获取验证码）
export const sendCodeService = (email: string, pictureCode?: string) => {
  console.log('发送注册验证码参数: email=', email, 'pictureCode=', pictureCode)
  if (pictureCode) {
    return request.get(`/code/sendRegister?email=${encodeURIComponent(email)}&pictureCode=${encodeURIComponent(pictureCode)}`)
  }
  return request.get(`/code/sendRegister?email=${encodeURIComponent(email)}`)
}

//提供发送登录验证码
export const sendLoginCodeService = (email: string, pictureCode?: string) => {
  console.log('发送登录验证码参数: email=', email, 'pictureCode=', pictureCode)
  // 发送登录验证码，使用GET请求
  if (pictureCode) {
    return request.get(`/code/sendLogin?email=${encodeURIComponent(email)}&pictureCode=${encodeURIComponent(pictureCode)}`)
  }
  return request.get(`/code/sendLogin?email=${encodeURIComponent(email)}`)
}

//提供验证码验证（用于验证邮箱和验证码）
export const verifyCodeService = (email: string, code: string) => {
  const params = new URLSearchParams()
  params.append('email', email)
  params.append('code', code)
  console.log('验证码验证参数:', params.toString())
  return request.post('/user/codeLogin', params)
}

//提供获取用户信息
export const getUserInfoService = () => {
  return request.get('/user/userInfo')
}

// 退出登录
export const logoutService = () => {
  return request.post('/user/logout')
}

// 更新用户基础资料（用户名、性别）
export const updateUserProfileService = (data: any) => {
  const params = new URLSearchParams()
  for (const key in data) {
    params.append(key, String(data[key]))
  }
  return request.put('/user/update', data)
}

// 上传头像（multipart/form-data）
export const uploadAvatarService = (file: File) => {
  const formData = new FormData()
  // 兼容大多数后端默认接收字段名：file
  formData.append('file', file)
  return request.post('/file/photoUpload', formData, {
    headers: { 'Content-Type': 'multipart/form-data' }
  })
 
}
//删除头像
export const deleteAvatarService = (avatarUrl: string) => {
  return request.delete(`/file/photoDelete?url=${avatarUrl}`)
}
//更新头像
export const updateAvatarService = (avatarUrl: string) => {
  return request.patch(`/user/updateAvatar?avatarUrl=${avatarUrl}`)
}

//提供更改密码发送验证码
export const sendResetCodeService = (email: string, pictureCode?: string) => {
  console.log('发送重置密码验证码参数: email=', email, 'pictureCode=', pictureCode)
  if (pictureCode) {
    return request.get(`/code/sendUpdatePwd?email=${encodeURIComponent(email)}&pictureCode=${encodeURIComponent(pictureCode)}`)
  }
  return request.get(`/code/sendUpdatePwd?email=${encodeURIComponent(email)}`)
}

//提供重置密码
export const resetPasswordService = (resetData: any) => {
  const params = new URLSearchParams()
  for (const key in resetData) {
    params.append(key, resetData[key])
  }
  // console.log(params.toString());

  return request.patch('/user/updatePwd', params)
}

// 获取图形验证码
export const getCaptchaService = (email: string) => {
  const url = `/api/code/kaptcha?email=${encodeURIComponent(email)}`
  console.log('生成的验证码URL:', url)
  return url
}



